package com.jeejio.common.base.mvvm

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.awesome.common.base.mvvm.BaseViewModel
import com.awesome.common.base.mvvm.ViewModelUtils
import com.awesome.common.util.ui.StatusBarUtil

/**
 * Author: JFangZ
 * Email:  janezhang1901@163.com
 * Date:2021/5/19 10:54
 * Description:The base of fragment.If want to use MVVM frame,please inherit this.
 */
abstract class BaseMVVMFragment<VM : BaseViewModel<*>, VB : ViewBinding> : Fragment() {
    protected var rootView: View? = null
    private var isInitialize: Boolean = false
    private var isFirst = true
    protected val mContext by lazy { context!! }
    protected val viewModel: VM by lazy { ViewModelUtils.createViewModel<VM>(this, 0) }
    protected val activityViewModel: VM by lazy { ViewModelUtils.createActivityViewModel<VM>(this, 0) }
    protected val viewBinding: VB by lazy { ViewModelUtils.createViewBinding(this, 1)!! }

    /**
     * 返回ID或者View 如果使用ViewBinding 返回null或者不实现
     */
    open fun initLayout(): Any? = null


    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Do UI init operation,such as findViewByID,setAdapter and so on.
     */
    open fun initView() {}

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Do setListener/setCallback operation.
     */
    open fun initListener() {}

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Do init observer of live data.
     */
    open fun initObserver() {}

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2019/4/8 11:34
     * Description:Do data init operation,such as network request,read database,read file and so on.
     */
    open fun initData() {}

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (rootView != null) return rootView
        // 获取 rootView
        when (val view = initLayout()) {
            is View -> rootView = view
            is Int -> rootView = LayoutInflater.from(context).inflate(view, container, false)
            else -> rootView = viewBinding.root
        }
        if (immersive() == true) {
            // 使内容延伸到状态栏下
            StatusBarUtil.setStatusBarTranslucent(activity?.window, true)
            // 使状态栏透明
            StatusBarUtil.setStatusBarColor(activity?.window, Color.TRANSPARENT, false)
        } else if (immersive() == false) {
            StatusBarUtil.setStatusBarTranslucent(activity?.window, false)
        }
        // 状态栏背景颜色
        initStatusBarColor()?.let {
            StatusBarUtil.setStatusBarColor(activity?.window, it, false)
            // 根据状态栏颜色自动切换状态栏文字的颜色
            StatusBarUtil.setAutoStatusBarStyle(activity?.window)
        }

        // 状态栏深色图标
        initStatusBarDark()?.let { StatusBarUtil.setStatusBarStyle(activity?.window, it) }
        // 导航栏颜色
        initNavigationBarColor()?.also {
            activity!!.window.navigationBarColor = it
        }
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (isInitialize) return
        isInitialize = true
        initView()
        initListener()
        initObserver()
        initData()
    }

    override fun onResume() {
        super.onResume()
        if (isFirst) {
            initLazy()
            isFirst = false
        }
    }


    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:39
     * Description:Make the activity immersive or not,if enable immersion,the status bar will transparent.
     *
     * @return Returns true to enable immersion,false not to enable immersion.
     */
    protected open fun immersive(): Boolean? = null

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:44
     * Description:Set the color of status bar.
     *
     * @return Color's value.
     */
    protected open fun initStatusBarColor(): Int? = null

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:45
     * Description:Set the mode of icon in status bar
     *
     * @return Returns true to start dark mode,false to start light mode.
     */
    protected open fun initStatusBarDark(): Boolean? = null

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:44
     * Description:Set the color of navigation bar.
     *
     * @return Color's value.
     */
    protected open fun initNavigationBarColor(): Int? = null

    protected open fun initLazy() {}

    protected open fun toastShort(content: String?) =
        Toast.makeText(context, content, Toast.LENGTH_SHORT).show()

    protected open fun toastLong(content: String?) =
        Toast.makeText(context, content, Toast.LENGTH_LONG).show()

    fun <T : View> findViewById(id: Int): T = view?.findViewById(id) as T
}